package thread;

import java.util.List;
import java.util.concurrent.*;

public class ThreadPoolExecutorDemo {

    void test(ThreadPoolExecutor executor) {
        for (int i = 0; i < 15; i++) {
            final int n = i;
            executor.execute(() -> {
                System.out.println(n + " running");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println(n + " sleep interrupted");
                }
                System.out.println(n + " exit");
            });
            System.out.println("submit " + n);
        }
        printInfo(executor);
    }

    void printInfo(ThreadPoolExecutor executor) {
        System.out.println("pool size -> " + executor.getPoolSize());
        System.out.println("queue size -> " + executor.getQueue().size());
        System.out.println("active count -> " + executor.getActiveCount());
        System.out.println("task count -> " + executor.getTaskCount());
    }

    /**
     * 核心5 最大10 无界队列 超时时间5S
     */
    void test1() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        test(executor);
        System.out.println("sleep 15s");
        Thread.sleep(15000);
        printInfo(executor);
        executor.shutdown();
    }

    /**
     * 核心5 最大10 队列3 超时时间5S 自定义拒绝策略
     * @throws InterruptedException
     */
    void test2() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                10, 5,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(3),
                (r, executor1) -> System.out.println("拒绝了一个任务"));
        test(executor);
        System.out.println("sleep 15s");
        Thread.sleep(15000);
        printInfo(executor);
        executor.shutdown();
    }

    /**
     * 核心5 最大5 无界队列 超时时间0
     */
    void test3() throws InterruptedException {
//        Executors.newFixedThreadPool(5);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                5, 0L,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        test(executor);
        System.out.println("sleep 15s");
        Thread.sleep(15000);
        printInfo(executor);
        executor.shutdown();
    }

    /**
     * 核心0 最大Max 同步队列 超时时间60
     */
    void test4() throws InterruptedException {
//        Executors.newCachedThreadPool();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(0,
                Integer.MAX_VALUE, 60L,
                TimeUnit.SECONDS, new SynchronousQueue<>());
        test(executor);
        System.out.println("sleep 15s");
        Thread.sleep(15000);
        printInfo(executor);
        System.out.println("sleep 60s");
        Thread.sleep(60000);
        printInfo(executor);
        executor.shutdown();
    }

    /**
     * Schedule
     */
    void test5() {
        ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor(2);
        executorService.schedule(() -> {
            System.out.println(System.currentTimeMillis());
        }, 3, TimeUnit.SECONDS);
        System.out.println(System.currentTimeMillis());
    }

    /**
     * Fix rate
     */
    void test6() {
        ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor(2);
        executorService.scheduleAtFixedRate(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("1 -> " + System.currentTimeMillis());
        }, 2, 1, TimeUnit.SECONDS);
        executorService.scheduleWithFixedDelay(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("\t\t\t\t\t\t\t\t\t\t2 -> " + System.currentTimeMillis());
        }, 2, 1, TimeUnit.SECONDS);
    }

    /**
     * shutdownNow
     */
    void test7() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                10, 5,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(3),
                (r, executor1) -> System.out.println("拒绝了一个任务"));
        test(executor);
        Thread.sleep(1000);
        List<Runnable> r = executor.shutdownNow();
        executor.execute(() -> {});
        System.out.println("队列中的任务：" + r.size());
    }

    /**
     * shutdown
     */
    void test8() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                10, 5,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(3),
                (r, executor1) -> System.out.println("拒绝了一个任务"));
        test(executor);
        Thread.sleep(1000);
        executor.shutdown();
        executor.execute(() -> {});
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutorDemo d = new ThreadPoolExecutorDemo();
//        d.test1();
//        d.test2();
//        d.test3();
//        d.test4();
//        d.test5();
//        d.test6();
        d.test7();
//        d.test8();
    }
}
